package com.aoxon.cargo.util;

import android.graphics.Bitmap;
import android.util.Log;
import android.view.View;

import com.aoxon.cargo.bean.ClosePoint;
import com.baidu.mapapi.map.LocationData;
import com.google.gson.Gson;

public class BMapUtil {

	private static final double PI = 3.14159265;
	private static final double EARTH_RADIUS = 6378137;
	private static final double RAD = Math.PI / 180.0;

	/**
	 * 从view 得到图片
	 * 
	 * @param view
	 * @return
	 */

	public static Bitmap getBitmapFromView(View view) {
		view.destroyDrawingCache();
		view.measure(View.MeasureSpec.makeMeasureSpec(0,
				View.MeasureSpec.UNSPECIFIED), View.MeasureSpec
				.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
		view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
		view.setDrawingCacheEnabled(true);
		Bitmap bitmap = view.getDrawingCache(true);
		return bitmap;
	}

	// @see
	// http://snipperize.todayclose.com/snippet/php/SQL-Query-to-Find-All-Retailers-Within-a-Given-Radius-of-a-Latitude-and-Longitude--65095/
	// The circumference of the earth is 24,901 miles.
	// 24,901/360 = 69.17 miles / degree

	/**
	 * @param raidus
	 *            单位米 return minLat,minLng,maxLat,maxLng
	 */

	public static ClosePoint getAround(LocationData data, int raidus) {
		double lat = data.latitude;
		double lon = data.longitude;
		ClosePoint cp = new ClosePoint();
		Double latitude = lat;
		Double longitude = lon;

		Double degree = (24901 * 1609) / 360.0;
		double raidusMile = raidus;

		Double dpmLat = 1 / degree;
		Double radiusLat = dpmLat * raidusMile;
		cp.setMin_latitude((int) ((latitude - radiusLat) * 1E6));
		cp.setMax_latitude((int) ((latitude + radiusLat) * 1E6));

		Double mpdLng = degree * Math.cos(latitude * (PI / 180));
		Double dpmLng = 1 / mpdLng;
		Double radiusLng = dpmLng * raidusMile;
		cp.setMin_longitude((int) ((longitude - radiusLng) * 1E6));
		cp.setMax_longitude((int) ((longitude + radiusLng) * 1E6));
		Log.e("ClosePoint", new Gson().toJson(cp));
		return cp;
	}

}
